<?php

class DefaultController extends MyAdminController
{
	public $modelName = 'Infopages';
	
	
	public function actionView($id)
	{
		$this->render('view',array(
			'model'=>$this->loadModel($id),
		));
	}


	public function actionIndex()
	{
		$this->getMaxSorter();
		$model = new $this->modelName('search');
		$model->resetScope();
		$model->unsetAttributes();  // clear any default values
		if(isset($_GET[$this->modelName])){
			foreach ($_GET[$this->modelName] as $key => $value)
			{
				$model->setAttribute($key, $value);
			}
		}
		$this->render('index', array_merge(array('model'=>$model), $this->params));
	}
	
	public function actionDelete($id)
	{
		if(Yii::app()->request->isPostRequest)
		{
			// we only allow deletion via POST request
			$this->loadModel($id)->delete();

			Yii::app()->user->setFlash('success', tt('Infopage successfully deleted'));
			// if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
			if(!isset($_GET['ajax']))
				$this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));
		}
		else
			throw new CHttpException(400,'Invalid request. Please do not repeat this request again.');
	}
	
	public function actionCreate()
	{
		$model = new $this->modelName;

		// Uncomment the following line if AJAX validation is needed
		// $this->performAjaxValidation($model);

		if(isset($_POST[$this->modelName]))
		{
			$model->attributes=$_POST[$this->modelName];
			if($model->save()) 
			{
				Yii::app()->user->setFlash('success', tt('Infopage successfully created'));
				$this->redirect(array('view','id'=>$model->id));
			}
		}

		$this->render('create',array('model'=>$model));
	}
	
	public function actionUpdate($id)
	{
		$model = $this->loadModel($id);

		// Uncomment the following line if AJAX validation is needed
		// $this->performAjaxValidation($model);

		if(isset($_POST[$this->modelName]))
		{
			$model->attributes = $_POST[$this->modelName];
			if($model->save())
			{
				Yii::app()->user->setFlash('success', tt('Infopage successfully updated'));
				$this->redirect(array('view','id'=>$model->id));
			}
		}

		$this->render('update',array('model'=>$model));
	}
	
	public function loadModel($id)
	{
		$model=Infopages::model()->findByPk((int)$id);
		if($model===null)
			throw new CHttpException(404,'The requested page does not exist.');
		return $model;
	}

	protected function performAjaxValidation($model)
	{
		if(isset($_POST['ajax']) && $_POST['ajax']==='infopages-form')
		{
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}
	}
}